<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 *  订单/商品补充
 */
class Supply extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];




    /**
     * @ApiTitle    (修改商品页面)
     * @ApiSummary  (修改商品页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Supply/goodsList)
     * @ApiParams   (name="id", type="varchar", required=false, description="商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {

    }
     * })
     */
    public function goodsList()
    {
        $id = $this->request->post('id');
        $res = Db::name('goods')->where('id',$id)->find();
        $game = Db::name('game')->where('id',$res['gameid'])->find();
        if ($res['is_safe'] == 1) {
            $fee = Db::name('server')->where('id',1)->find();
            $res['price'] = round($res['price']/(1+$fee['service']),2);
        }
        $res['img'] = config('api_image_url') . $game['image'];
        $res['gameType'] = $game['type'];
        $res['image'] = config('api_image_url') . $res['image'];
        $res['images'] = str_replace('/uploads',config('api_image_url').'/uploads',$res['images']);
        $res['sku'] = htmlspecialchars_decode($res['sku']);
        $res['sku'] = json_decode($res['sku'],true);
        $res['info'] = htmlspecialchars_decode($res['info']);
        $res['info'] = json_decode($res['info'],true);
        $this->success('ok',$res);
    }



	/**
	 * @ApiTitle    (客服介入)
	 * @ApiSummary  (客服介入)
	 * @ApiMethod   (POST)
	 * @ApiRoute    (api/Supply/intervention)
	 * @ApiParams   (name="id", type="varchar", required=false, description="订单id")
	 * @ApiParams   (name="type", type="varchar", required=false, description="1:买家介入 2:卖家介入")
	 * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
	 * @ApiReturn   ({
	"code": 1,
	"msg": "ok",
	"time": "1627553755",
	"data": {

	}
	 * })
	 */
    public function intervention()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $res = Db::name('order')->where('id',$id)->value('intervention');
        if ($res != 0) {
            $data = Db::name('order')->where('id',$id)->update(['intervention'=>3]);
        } else {
            $data = Db::name('order')->where('id',$id)->update(['intervention'=>$type]);
        }
        if ($data) {
            $this->success('介入成功');
        } else {
            $this->error('介入失败');
        }
    }


	/**
	 * @ApiTitle    (聊天提示)
	 * @ApiSummary  (聊天提示)
	 * @ApiMethod   (POST)
	 * @ApiRoute    (api/Supply/tips)
	 * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
	 * @ApiParams   (name="id", type="varchar", required=false, description="订单id")
	 * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
	 * @ApiReturn   ({
	"code": 1,
	"msg": "ok",
	"time": "1627553755",
	"data": {

	}
	 * })
	 */
    public function tips()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $oid = $this->request->post('id');
        $type = $this->request->post('type');
        $res = Db::name('order')->where('id',$oid)->find();
        if (!empty($type)) {
            $ret = Db::name('reorder')->where('id',$oid)->find();
            if ($ret['buyid'] == $id) {
                $this->success('ok','six');
            } elseif ($ret['uid'] == $id) {
                $this->success('ok','seven');
            }
        }
        if ($res['buyid'] == $id) {
            if ($res['status'] == 2 || $res['status'] == 3) {
                $this->success('ok','one');
            } else {
                $this->success('ok','two');
            }
        } elseif ($res['uid'] == $id) {
            if ($res['status'] == 2) {
                $this->success('ok','three');
            } elseif ($res['status'] == 3 || $res['status'] == 11 || $res['status'] == 12) {
                $this->success('ok','four');
            } else {
                $this->success('ok','five');
            }
        } else {
            $this->success('ok',0);
        }
    }



    /**
     * @ApiTitle    (商品编号查找商品)
     * @ApiSummary  (商品编号查找商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Supply/codeSearch)
     * @ApiParams   (name="goods_sn", type="varchar", required=false, description="商品编号")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        id
    }
     * })
     */
    public function codeSearch()
    {
        $goods_sn = $this->request->post('goods_sn');
        $res = Db::name('goods')->where('goods_sn',$goods_sn)->value('id');
        if ($res) {
            $this->success('ok',$res);
        } else {
            $this->error('没有该商品编号');
        }
    }


	/**
	 * @ApiTitle    (获取单位后缀)
	 * @ApiSummary  (获取单位后缀)
	 * @ApiMethod   (POST)
	 * @ApiRoute    (api/Supply/specs)
	 * @ApiParams   (name="goods_type", type="varchar", required=false, description="游戏类型id")
	 * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
	 * @ApiReturn   ({
	"code": 1,
	"msg": "ok",
	"time": "1627553755",
	"data": {

	}
	 * })
	 */
    public function specs()
    {
	    $type = $this->request->post('goods_type');
	    $typeName = Db::name('game_info')->where('id',$type)->value('name');
	    $res = Db::name('attr')->where('type',$typeName)->value('value');
	    $this->success('ok',$res);
    }



	/**
	 * @ApiTitle    (充值)
	 * @ApiSummary  (充值)
	 * @ApiMethod   (POST)
	 * @ApiRoute    (api/Supply/recharge)
	 * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
	 * @ApiParams   (name="type", type="varchar", required=true, description="支付类型 微信:wechat 支付宝:alipay")
	 * @ApiParams   (name="amount", type="varchar", required=true, description="充值金额")
	 * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
	 * @ApiReturn   ({
	"code": 1,
	"msg": "ok",
	"time": "1627553755",
	"data": {

	}
	 * })
	 */
    public function recharge()
    {
	    $token = $this->request->header('token');
	    $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
	    $type = $this->request->post('type');
	    $price = $this->request->post('amount');
	    if ($price < 1) {
		    $this->error(__('充值金额不小于￥1'));
	    }
	    if ($type == 'alipay') {
	    	$pay_type = '支付宝';
	    } else {
		    $pay_type = '微信';
	    }
	    $sn = 'R' . $this->getRefundSn($id);
	    $res = [
	    	'uid'   =>  $id,
		    'log_sn'    => $sn,
		    'amount'    =>  $price,
		    'type'  =>  $pay_type,
		    'createtime'    =>  time(),
		    'status'    =>  0
	    ];
		Db::name('recharge')->insert($res);

	    //        $amount = $price;
	    $amount = 0.01;
	    $orderid = $sn;
	    $title = "780g";
	    $notifyurl = $this->request->root(true).'/api/pay/notifyx/paytype/' . $type;
	    $returnurl = $this->request->root(true) . '/addons/epay/index/returnr/paytype/' . $type;
	    $method = "web";
	    $a = \addons\epay\library\Service::submitOrder($amount,$orderid,$type,$title,$notifyurl,$returnurl,$method);
	    return $a;
    }
}
